package com.xiaoyun.controller;

import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.spring.boot.core.PayServiceManager;
import com.egzosn.pay.spring.boot.core.bean.MerchantPayOrder;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.xiaoyun.bo.MerchantOrderBO;
import com.xiaoyun.constant.Const;
import com.xiaoyun.enums.PaymentStatus;
import com.xiaoyun.pojo.PayOrders;
import com.xiaoyun.service.PaymentOrderService;
import com.xiaoyun.utils.RedisUtil;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.PaymentInfoVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequiredArgsConstructor
public class PaymentController extends BaseController {
    private final PayServiceManager manager;

    private final PaymentOrderService paymentOrderService;

    private final RedisUtil redisUtil;

    @PostMapping("getPaymentCenterOrderInfo")
    public XYJSONResult getPaymentCenterOrderInfo(String merchantOrderId, String merchantUserId) {
        if (StringUtils.isBlank(merchantOrderId) || StringUtils.isBlank(merchantUserId)) {
            return XYJSONResult.errorMsg("");
        }
        PayOrders orderInfo = paymentOrderService.queryOrderInfo(merchantUserId, merchantOrderId);

        return XYJSONResult.ok(orderInfo);
    }

    @GetMapping("goAlipay")
    public XYJSONResult goAlipay(String merchantOrderId, String merchantUserId) throws IOException {
        PayOrders waitPayOrder = paymentOrderService.queryOrderByStatus(merchantOrderId, merchantUserId,
                PaymentStatus.WAIT_PAY.type);
        String subject = "天天吃货-付款用户[" + merchantUserId + "]";
        String out_trade_no = merchantOrderId;
        if (waitPayOrder != null) {
            String qrCodeUrl = redisUtil.getKey(Const.AliQrCodeKey + ":" + merchantOrderId);
            if (StringUtils.isEmpty(qrCodeUrl)) {
                MerchantPayOrder payOrder = new MerchantPayOrder(Const.ALI_PAY, AliTransactionType.SWEEPPAY.getType(),
                        subject, subject, new BigDecimal(waitPayOrder.getAmount() / 100), out_trade_no);
                qrCodeUrl = manager.toPay(payOrder);
            }
            PaymentInfoVO paymentInfoVO = new PaymentInfoVO();
            paymentInfoVO.setAmount(waitPayOrder.getAmount());
            paymentInfoVO.setMerchantOrderId(merchantOrderId);
            paymentInfoVO.setMerchantUserId(merchantUserId);
            paymentInfoVO.setQrCodeUrl(qrCodeUrl);
            redisUtil.setKey(Const.AliQrCodeKey + ":" + merchantOrderId, paymentInfoVO, 2, TimeUnit.HOURS);
            return XYJSONResult.ok(qrCodeUrl);
        } else {
            return XYJSONResult.errorMsg("订单不存在,或者已经支付");
        }
    }

    @PostMapping("getWxPayQRCode")
    public XYJSONResult getWxPayQRCode(String merchantOrderId, String merchantUserId) throws IOException {
        PayOrders waitPayOrder = paymentOrderService.queryOrderByStatus(merchantOrderId, merchantUserId,
                PaymentStatus.WAIT_PAY.type);
        String subject = "天天吃货-付款用户[" + merchantUserId + "]";
        String out_trade_no = merchantOrderId;
        if (waitPayOrder != null) {
            String qrCodeUrl = redisUtil.getKey(Const.WxQrCodeKey + ":" + merchantOrderId);
            if (StringUtils.isEmpty(qrCodeUrl)) {
                MerchantPayOrder payOrder = new MerchantPayOrder(Const.WX_PAY, WxTransactionType.NATIVE.getType(),
                        subject, subject, new BigDecimal(waitPayOrder.getAmount() / 100), out_trade_no);
                qrCodeUrl = manager.getQrPay(payOrder);
            }
            PaymentInfoVO paymentInfoVO = new PaymentInfoVO();
            paymentInfoVO.setAmount(waitPayOrder.getAmount());
            paymentInfoVO.setMerchantOrderId(merchantOrderId);
            paymentInfoVO.setMerchantUserId(merchantUserId);
            paymentInfoVO.setQrCodeUrl(qrCodeUrl);
            redisUtil.setKey(Const.WxQrCodeKey + ":" + merchantOrderId, paymentInfoVO, 2, TimeUnit.HOURS);
            return XYJSONResult.ok(paymentInfoVO);
        } else {
            return XYJSONResult.errorMsg("订单不存在,或者已经支付");
        }
    }

    @PostMapping("createMerchantOrder")
    public XYJSONResult createMerchanOrder(@RequestBody @Valid MerchantOrderBO merchantOrderBO) {
        boolean isSuccess = false;
        try {
            isSuccess = paymentOrderService.createPaymentOrder(merchantOrderBO);
        } catch (Exception e) {
            e.printStackTrace();
            XYJSONResult.errorMsg(e.getMessage());
        }
        if (isSuccess) {
            return XYJSONResult.ok("创建订单成功");
        } else {
            return XYJSONResult.errorMsg("创建订单失败");
        }

    }
}
